package alignment_text_input;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import com.microsoft.azure.storage.CloudStorageAccount;
import com.microsoft.azure.storage.blob.CloudBlobClient;
import com.microsoft.azure.storage.blob.CloudBlobContainer;
import com.microsoft.azure.storage.blob.CloudBlobDirectory;
import com.microsoft.azure.storage.blob.CloudBlockBlob;
import com.microsoft.azure.storage.blob.ListBlobItem;

public class DownloadOutputAzure {
	private static File readFile(String nameOfFile, String outputAzure) throws Exception {
		File path = new File(nameOfFile);
		path.mkdir();
		
		final String storageConnectionString = "DefaultEndpointsProtocol=http;" + "AccountName=testematcher;"
				+ "AccountKey=TqIn51pzE5r8CyAv0QeTTBqtd7LbS7bksSv/C8CMXAzvabqjtobID4qk5rw9yiY55p4mx5pwUC0/I1l7b19Zqw==";

		CloudStorageAccount account = CloudStorageAccount.parse(storageConnectionString);
		CloudBlobClient serviceClient = account.createCloudBlobClient();

		// Container name must be lower case.
		CloudBlobContainer container = serviceClient.getContainerReference("superclusterlqd");
		// container.createIfNotExists();
		CloudBlobDirectory sdproj = container.getDirectoryReference("SDProject").getSubDirectoryReference(outputAzure);
		
		for (ListBlobItem blob : sdproj.listBlobs()) {
			String[] pathOfAzureFile = blob.getUri().getPath().split("/");
			String azureFile = pathOfAzureFile[pathOfAzureFile.length - 1];
			
			CloudBlockBlob arq = sdproj.getBlockBlobReference(azureFile);
			if (!arq.getName().contains("SUCCESS")) {
				File destinationFile = new File(path.getAbsolutePath() + "\\" + azureFile + ".txt");
				arq.downloadToFile(destinationFile.getAbsolutePath());
			}
			
		}
		
		return path;
	}
	
	public static ArrayList<String> getAlignment(String outputAzure) {
		ArrayList<String> align = new ArrayList<String>();
		try {
			File outputFile = readFile("OutputAzure", outputAzure);
			String[] children = outputFile.list();  
			if (children == null) {  
			    // Either dir does not exist or is not a directory  
			} else {  
			    for (int i=0; i<children.length; i++) {  
			        // Get filename of file or directory  
			        File output = new File(outputFile.getAbsolutePath() + "\\" + children[i]);
			        align.addAll(readTxtFile(output));
			    }  
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return align;
		
	}

	private static ArrayList<String> readTxtFile(File output) throws IOException {
		ArrayList<String> align = new ArrayList<String>();
		FileReader reader =  new FileReader(output);
		BufferedReader readArq = new BufferedReader(reader);
		
		readArq.readLine();
		String concepts = readArq.readLine();
		while (concepts != null) {
			if (!concepts.isEmpty()) {
				concepts = concepts.split(":")[1].split("=")[0].trim();
				align.add(concepts);
			}
			concepts = readArq.readLine();
		}
		
		readArq.close();
		reader.close();
		return align;
	}

	public static ArrayList<String> readInPath(String outputFile) {
		ArrayList<String> align = new ArrayList<String>();
		File arquivo = new File(outputFile);  
	       
		File[] files = arquivo.listFiles();
		
		for (File file : files) {
			try {
				align.addAll(readTxtFile(file));
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}		
		
		return align;
	}

}
